package com.shundr.shipper.common.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.shundr.shipper.R;
import com.shundr.shipper.cargo.model.CargoInfo;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.user.http.UserRequest;

public class CallPhoneToShipper {
	Context mContext;
	String mCargoId;
	private String[] mPhoneNumbers;
	private mOnCallPhoneFinishListener mOnCallPhoneFinishListener;

	public CallPhoneToShipper(Context context, String cargoId) {
		this.mContext = context;
		this.mCargoId = cargoId;
		new UserRequest(context, mHandler).grabOrder(mCargoId);
	}
	
	public void setmOnCallPhoneFinishListener(
			mOnCallPhoneFinishListener mOnCallPhoneFinishListener) {
		this.mOnCallPhoneFinishListener = mOnCallPhoneFinishListener;
	}

	public interface mOnCallPhoneFinishListener{
		public void OnCallPhoneListener();
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.GRAB_ORDER_SUCCESSED:
				CargoInfo cargoInfo = (CargoInfo) msg.obj;
				getPhoneNumber(cargoInfo);
				break;
			case UserRequest.REQUEST_FAILED:
				String description = (String) msg.obj;
				ToastHelper.toastShort(mContext, description);
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	

	/**
	 * 获取号码
	 * 
	 * @param cargoInfo
	 */
	private void getPhoneNumber(CargoInfo cargoInfo) {
		mPhoneNumbers = new String[] { cargoInfo.getCargoCompanyPhone1(),
				cargoInfo.getCargoCompanyPhone2(),
				cargoInfo.getCargoCompanyPhone3(),
				cargoInfo.getCargoCompanyPhone4() };
		// 可用电话个数
		int num = 0;
		int p = 0;
		for (int i = 0; i < mPhoneNumbers.length; i++) {
			if (mPhoneNumbers[i] != null) {
				num++;
				p = i;
			}
		}
		if (mPhoneNumbers == null || mPhoneNumbers.length <= 0) {
			ToastHelper.toastShort(mContext, "抱歉，该货主未留电话");
		} else if (num == 1) {
			String mobileText = mPhoneNumbers[p].replace("-", "");
			Intent intent = new Intent();// 创建一个意图对象，用来激发拨号的Activity
			intent.setAction("android.intent.action.CALL");
			intent.setData(Uri.parse("tel:" + mobileText));
			mContext.startActivity(intent);// 方法内部会自动添加类别,android.intent.category.DEFAULT
			mOnCallPhoneFinishListener.OnCallPhoneListener();
			LContext.isOrdered = true;
		} else if (num > 1) {
			DialogPhoneList DialogPhoneList = new DialogPhoneList(mContext);
			// Window win = phoneDialog.getWindow();
			// LayoutParams params = new LayoutParams();
			// params.x = -90;// 设置x坐标
			// params.y = 160;// 设置y坐标
			// win.setAttributes(params);
			DialogPhoneList.setCanceledOnTouchOutside(true);// 设置点击Dialog外部任意区域关闭Dialog
			DialogPhoneList.show();
		}
	}

	class DialogPhoneList extends Dialog {
		Context context;
		List<? extends Map<String, ?>> mList;
		ListView mListView;
		public DialogPhoneList(Context context) {
			super(context, R.style.dialog_phone_num_list);
			this.context = context;
		}

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.view_dialog_phone_list);
			mListView = (ListView) findViewById(R.id.lv_dialog_phone_listview);
			mList = getData();
			SimpleAdapter adapter = new SimpleAdapter(context, mList,
					R.layout.view_dialog_phone_list_item,
					new String[] { "data" }, new int[] { R.id.tv_phone_num });
			mListView.setAdapter(adapter);
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					String mobileText = mPhoneNumbers[position].replace("-", "");
					Intent intent = new Intent();// 创建一个意图对象，用来激发拨号的Activity
					intent.setAction("android.intent.action.CALL");
					intent.setData(Uri.parse("tel:" + mobileText));
					context.startActivity(intent);// 方法内部会自动添加类别,android.intent.category.DEFAULT
					mOnCallPhoneFinishListener.OnCallPhoneListener();
					LContext.isOrdered = true;
					dismiss();
				}
			});
		}

		private List<HashMap<String, Object>> getData() {// String[] datas) {
			List<HashMap<String, Object>> dataList = new ArrayList<HashMap<String, Object>>();
			if (mPhoneNumbers == null)
				return dataList;
			for (int i = 0; i < mPhoneNumbers.length; i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				if (mPhoneNumbers[i] == null || mPhoneNumbers[i].length() <= 0)
					continue;
				map.put("data", mPhoneNumbers[i]);
				dataList.add(map);
			}
			return dataList;
		}
	}
}
